//
//  TeachBottomView.m
//  fitnessmanage
//
//  Created by wujian on 2018/1/15.
//  Copyright © 2018年 zongquankeji. All rights reserved.
//

#import "TeachBottomView.h"
#import "TeachLeftView.h"

@interface TeachBottomView()

@property(nonatomic,strong)NSMutableArray *viewArray;

@end

@implementation TeachBottomView

-(id)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    
    if (self) {
                        
        UIButton *functionBtn = [[UIButton alloc] init];
        [functionBtn setImage:@"icn_add_course"];
        [self addSubview:functionBtn];
        self.functionBtn = functionBtn;
        
        self.viewArray = [[NSMutableArray alloc] init];
        
        [self setConstraints];
    }
    
    return self;
}


-(void)setConstraints{
    
    if (self.viewArray.count == 0) {
        
        [self.functionBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
            
            make.edges.mas_equalTo(self);
            
        }];
    }
    else{
        
        [self.functionBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
           
            make.right.bottom.mas_equalTo(self);
            
        }];
        
        TeachLeftView *tmpView = nil;
        
        int i = 0;
        
        for (TeachLeftView *imageView in self.viewArray) {
            
            if (tmpView) {
                
                [imageView mas_remakeConstraints:^(MASConstraintMaker *make) {
                    
                    if (i == self.viewArray.count - 1) {
                        
                        make.top.mas_equalTo(self);
                    }
                    
                    make.bottom.mas_equalTo(tmpView.mas_top).offset(-10);
                    
                    make.right.mas_equalTo(self.functionBtn).offset(-8);
                    
                    make.left.mas_equalTo(self);
                }];
            }
            else{
                
                [imageView mas_remakeConstraints:^(MASConstraintMaker *make) {
                    
                    if (i == self.viewArray.count - 1) {
                        
                        make.top.mas_equalTo(self);
                    }
                    
                    make.bottom.mas_equalTo(self.functionBtn.mas_top).offset(-10);
                    
                    make.right.mas_equalTo(self.functionBtn).offset(-8);
                    
                    make.left.mas_equalTo(self);
                }];
            }
            
            tmpView = imageView;
            
            i++;
        }
    }
    
}

-(void)setTitlesArray:(NSMutableArray *)titlesArray{
    
    _titlesArray = titlesArray;
    
    int i = 0;
    
    for (NSString *str in titlesArray) {
        
        TeachLeftView *leftView = [[TeachLeftView alloc] init];
        
        leftView.imageName = str;
        
        [leftView.imageView addTarget:self action:@selector(btnClick:)];
        
        leftView.imageView.tag = i;
        
        [self addSubview:leftView];
        
        [self.viewArray addObject:leftView];
    }
    
    [self setConstraints];
}

-(void)btnClick:(UIButton *)btn{
    
    NSString *title = [self.titlesArray objectAtIndex:btn.tag];
    
    if ([self.teachBottomViewDelegate respondsToSelector:@selector(btnClickWithTitle:)]) {
        
        [self.teachBottomViewDelegate btnClickWithTitle:title];
    }
}


-(void)clearViews{
    
    for (TeachLeftView *imageView in self.viewArray) {
        
        [imageView removeFromSuperview];
    }
    
    [self.viewArray removeAllObjects];
    
    [self setConstraints];
}

@end
